/* Trigger Name: OORTelesalesLeadTrigger 
 * Author: Accenture
 * Date: 13-May-2012
 * Requirement/Project Name: OORTelesales
 * Requirement Description: 
 **/

trigger OORTelesalesLeadTrigger on Lead (before insert, before update ,after update ,after insert) 
{
   final List<Lead>  updateZipCodeList =  new List<Lead>();
   final Set<String> zipCodeSet  = new Set<String>();
   final Set<ID> leadID = new Set<Id>();
   final Set<ID> campaginID = new Set<Id>();
   
   List<Lead> trigNewList = null;    
   
   trigNewList = OORTelesalesUtility.filterSobjectBasedOnRecType(Trigger.new,'Lead', OORTelesalesTextConstant.LOCAL_ADVERTISER_LEAD_REC_TYPE);        
             
   // Check if trigger has fired befor event
   if(Trigger.isbefore)
   {  
       // Trigger has fired before insert
        if(Trigger.isInsert)
        {           
           for(Lead leadObj : trigNewList)
           {
                // Check if Zip code is populated or not to get time zone.
                 if(leadObj.PostalCode != null && leadObj.PostalCode != '')
                 {
                       updateZipCodeList.add(leadObj);
                       zipCodeSet.add(leadObj.PostalCode);
                 }
            }
        }
        
        // Trigger has fired before Update.
        if(Trigger.isUpdate)
        {
            for(Lead leadObj : trigNewList )
            {                
                // Check if zip code is changed.
                if(Trigger.newMap.get(leadObj.id).PostalCode  != Trigger.oldMap.get(leadObj.id).PostalCode)
                {
                    leadObj.Time_Zone__c = '';
                    
                    // check if zip code is blank.
                    if(leadObj.PostalCode != null &&  leadObj.PostalCode != '')
                    {
                        updateZipCodeList.add(leadObj);
                        zipCodeSet.add(leadObj.PostalCode);
                    }        
                }
            }
        }
        // Assign the time zone to lead.
        OORTelesalesTriggerHandler.updateTimeZone(updateZipCodeList,zipCodeSet);     
    }
    
    // Check if trigger has fired befor event
    if(Trigger.isAfter)
    {
        // Trigger has fired after insert      
        if(Trigger.isInsert)
        {
            for(Lead leadObj : trigNewList)
            {
                leadID.add(leadObj.id);
                if(leadObj.OORTelesalesCampaign__c != NULL)
                campaginID.add(leadObj.OORTelesalesCampaign__c);
             }
        }
      
        // Trigger has fired after Update.
        if(Trigger.isUpdate)
        {
             for(Lead leadObj : trigNewList)
             {
                  // Check if lead Status is changed.
                  if( Trigger.newMap.get(leadObj.id).Lead_Status_ReadOnly__c!= Trigger.oldMap.get(leadObj.id).Lead_Status_ReadOnly__c || 
                      Trigger.newMap.get(leadObj.id).OORTelesalesCampaign__c!= Trigger.oldMap.get(leadObj.id).OORTelesalesCampaign__c)
                  {
                       leadID.add(leadObj.id);
                       if(leadObj.OORTelesalesCampaign__c != NULL)
                       campaginID.add(leadObj.OORTelesalesCampaign__c);                  
                  }
             }
        }
        
        OORTelesalesTriggerHandler.updateLeadStatusOnCampaignMember(leadID,campaginID,Trigger.newMap);
    }
    
    
    
    
             
}